# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  
  # Generates the HTML for top menu navigation
  def generate_top_menu(current_controller,project)
    resource_name_pairs = [[:projects,'Overview'],[:tasks,'Tasks'],[:members,'Team Members'],[:patterns,'Project Patterns'],[:documents,'Documents'],[:notes,'Notes'],  [:contacts,'Contacts'], [:users,'Users']]
    menu_html = '<ul class="tabs">'
    resource_name_pairs.each do |resource|
      in_menu_controller_name = resource[0]
      link_name = resource[1]

      if project && project.id
        if in_menu_controller_name == :projects
          link_url = project_url(project)

        elsif in_menu_controller_name==:users
          link_url = url_for(:controller=>:users)

        elsif in_menu_controller_name==:patterns
          next

        else
          link_url =  eval("project_#{in_menu_controller_name}_url(project)")

        end
      else
        next if in_menu_controller_name == :members || in_menu_controller_name==:tasks
        link_url = url_for(:controller => in_menu_controller_name)

      end

      html_class = 'selected' if current_controller == in_menu_controller_name.to_s
      menu_html += "<li>" + link_to(link_name, link_url,:class => html_class ) + "</li>\n"
    end
    menu_html += '</ul>'
  end

  # Displays flash messages for notices and errors, errors for default.
  def display_messages_for_all(msg_type="error")
    case msg_type
    when "notice"
      "<div class='notice'>" +  flash[:notice] + "</div>" if flash[:notice]
    when "error"
      "<div class='error'>" +  flash[:error] + "</div>" if flash[:error]
    else
      "<div class='notice'>" +  flash[:notice] + "</div>" if flash[:notice]
      "<div class='error'>" +  flash[:error] + "</div>" if flash[:error]
    end
  end

  # Formats given number as delimited, thousands seperated and with 2 decimal digits.
  def format_number (number)
    new_number = number.to_f
    new_number  = number_with_delimiter(sprintf("%d",new_number)  , ',', '.')
    new_number 
  end

end